package xyz.erupt.flow.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.erupt.core.annotation.EruptRouter;
import xyz.erupt.core.constant.EruptRestPath;
import xyz.erupt.core.view.EruptApiModel;
import xyz.erupt.flow.bean.entity.OaWorkFlowGroup;
import xyz.erupt.flow.constant.FlowConstant;
import xyz.erupt.flow.data.mapper.OaWorkFlowGroupMapper;
import xyz.erupt.flow.data.vo.VO_OaWorkFlowGroup;
import xyz.erupt.flow.service.WorkFlowGroupService;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @description:
 * @author: PP
 * @date: 2024/10/20 20:29
 * @param:
 * @return:
**/
@RestController
@RequestMapping(EruptRestPath.ERUPT_API + "/" + FlowConstant.SERVER_NAME)
public class WorkFlowGroupController {

    @Autowired
    private WorkFlowGroupService workFlowGroupService;

    /**
     * 查询所有表单分组
     * @return
     */
    @GetMapping("/admin/def/group")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel getGroups(@RequestParam(value = "keywords",required = false) String keywords){
        List<OaWorkFlowGroup> groups = workFlowGroupService.getGroups(keywords);
        List<VO_OaWorkFlowGroup> vo = OaWorkFlowGroupMapper.INSTANCE.toVo(groups);
        return EruptApiModel.successApi(vo);
    }


    /**
     * 表单分组排序
     * @param groups 分组数据
     * @return 排序结果
     */
    @PutMapping("/admin/def/group/sort")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel GroupsSort(@RequestBody @NotNull List<Long> groups){
        workFlowGroupService.GroupsSort(groups);
        return EruptApiModel.successApi();
    }

    /**
     * 修改分组
     * @param id 分组ID
     * @param name 分组名
     * @return 修改结果
     */
    @PutMapping("/admin/def/group")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel updateGroupName(@RequestParam @NotNull Long id,
                                       @RequestParam @NotBlank String name){
        workFlowGroupService.updateGroupName(id, name);
        return EruptApiModel.successApi();
    }

    /**
     * 新增表单分组
     * @param groupName 分组名
     * @return 添加结果
     */
    @PostMapping("/admin/def/group")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel createGroup(@RequestParam @NotBlank String groupName){
        workFlowGroupService.createGroup(groupName);
        return EruptApiModel.successApi();
    }

    /**
     * 删除分组
     * @param groupId 分组ID
     * @return 删除结果
     */
    @DeleteMapping("/admin/def/group/{groupId}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel deleteGroup(@PathVariable Long groupId){
        workFlowGroupService.deleteGroup(groupId);
        return EruptApiModel.successApi();
    }
}
